<template>
  <div class="clearfix">
    <a-upload
      :action="action"
      list-type="picture-card"
      :file-list="fileList"
      @preview="handlePreview"
      @change="handleChange"
    >
      <div v-if="fileList.length < limit">
        <a-icon type="plus" />
        <div class="ant-upload-text">
          上传照片
        </div>
      </div>
    </a-upload>
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </div>
</template>
<script>
import { UPLOAD_URL } from '@/const/const'
function getBase64 (file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = () => resolve(reader.result)
    reader.onerror = error => reject(error)
  })
}
export default {
  props: {
    action: {
      type: String,
      default: process.env.VUE_APP_API_BASE_URL + UPLOAD_URL
    },
    value: {
      type: String,
      default: ''
    },
    limit: {
      type: Number,
      default: 6
    },
    randomNum: {
      type: [String, Number],
      default: ''
    }
  },
  data () {
    return {
      previewVisible: false,
      previewImage: '',
      fileList: [],
    }
  },
  watch: {
    value () {
      const list = []
      if (this.value != '') {
        this.value.split(',').forEach((item, index) => {
          list.push({
            uid: index,
            name: 'image.png',
            status: 'done',
            url: item
          })
        })
      }
      this.fileList = list
      console.log(this.fileList)
    },
    randomNum (newVal) {
      console.log(newVal)
      const list = []
      if (this.value != '') {
        this.value.split(',').forEach((item, index) => {
          list.push({
            uid: index,
            name: 'image.png',
            status: 'done',
            url: item
          })
        })
      }
      this.fileList = list
      console.log(this.fileList)
    }
  },
  methods: {
    handleCancel () {
      this.previewVisible = false
    },
    async handlePreview (file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj)
      }
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },
    handleChange ({ fileList }) {
      this.fileList = fileList
      const imgList = []
      this.fileList.forEach(item => {
        if (item.status == 'done') {
          if (item.response) {
            imgList.push(item.response.data)
          } else {
            imgList.push(item.url)
          }
        }
      })
      console.log(imgList)
      this.$emit('input', imgList.join(','))
    },
  },
}
</script>
<style>
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>